package com.auxgroup.smarthome.business.controller;

import com.auxgroup.bridge.business.inner.dto.AppPushTemplateDto;
import com.auxgroup.bridge.business.inner.vo.AppPushTemplateVo;
import com.auxgroup.smarthome.business.service.inner.AppPushTemplateInnerService;
import com.auxgroup.smarthome.syscode.Syscode;
import com.auxgroup.smarthome.utils.PageContent;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * Created by niuGuangzhe on 2017/8/7.
 */
@Api(description = "推送模板", tags = "推送模板相关")
@RestController
public class ConsumerPushTemplateController {

    @Autowired
    private AppPushTemplateInnerService appPushTemplateInnerService;

    @ApiOperation(value = "推送模板-查询", notes = "推送模板-查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词(标题&内容)", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "pushTimeStart", value = "开始推送时间 非必填 格式严格按照‘yyyy-MM-dd HH:mm’", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "pushTimeEnd", value = "结束推送时间 非必填 格式严格按照‘yyyy-MM-dd HH:mm’", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "progress", value = "0:已保存 1：已发送 2：已删除 暂时保留，不传递参数默认返回（已发送&已保存）", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/push/templates", method = RequestMethod.GET)
    public ApiResponse<Page<AppPushTemplateVo>> getAppPushTemplate(@RequestParam(required = false) String keyword,
                                                                   @RequestParam(required = false) String pushTimeStart,
                                                                   @RequestParam(required = false) String pushTimeEnd,
                                                                   @RequestParam(required = false) Integer progress,
                                                                   @RequestParam(defaultValue = "0", required = false) Integer page,
                                                                   @RequestParam(defaultValue = "10", required = false) Integer size) {

        ApiResponse<PageContent<AppPushTemplateVo>> apiResponse = appPushTemplateInnerService.findAppPushTemplate(keyword, pushTimeStart, pushTimeEnd, progress, page, size);
        Page<AppPushTemplateVo> pageContent = apiResponse.getData().convertPage();
        return new ApiResponse(apiResponse.getCode(), apiResponse.getMessage(), pageContent);
    }


    @ApiOperation(value = "推送模版-新增", notes = "推送模板-新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateTitle", value = "模板标题", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "templateContent", value = "模板内容", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "templateCondition", value = "推送条件 json格式 非必填如果要填写请严格按照{\"citys\":{\"广东\":\"广州\",\"四川\":\"成都\"},\"gender\":\"M\"}的格式填写,其中citys为map映射,若为空\"citys\":{},，目标用户的性别gender取值范围 M:男 F：女 N：未知", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "templateUri", value = "推送超链接", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "customVariable", value = "自定义变量(${username},...)", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "pushType", value = "推送类型(0:立即推送 1:定时推送)", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "pushTime", value = "当上边的推送类型为定时推送时，此项为必填 推送时间格式(yyyy-MM-dd HH:mm)", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "imgUri", value = "推送图片链接", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "adCondition", value = "广告json", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "templateType", value = "模板类型", paramType = "form", dataType = "string")

    })
    @RequestMapping(value = "/push/template", method = RequestMethod.POST)
    public ApiResponse addAppPushTemplate(@ApiIgnore @Valid AppPushTemplateDto dto) {
        return appPushTemplateInnerService.addAppPushTemplate(dto);
    }


    @ApiOperation(value = "推送模版-修改", notes = "推送模板-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "主键", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "templateTitle", value = "模板标题", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "templateContent", value = "模板内容", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "templateCondition", value = "推送条件 json格式 非必填如果要填写请严格按照{\"gender\":\"M\",\"region\":\"广东\",\"city\":\"广州\"}的格式填写", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "templateUri", value = "推送超链接", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "customVariable", value = "自定义变量", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "pushType", value = "推送类型(0:立即推送 1:定时推送)", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "pushTime", value = "当推送类型为立即推送时 此项为必填，推送时间格式(yyyy-MM-dd HH:mm)", paramType = "form", dataType = "date"),
            @ApiImplicitParam(name = "imgUri", value = "推送图片链接", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "adCondition", value = "广告json", paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "templateType", value = "模板类型", paramType = "form", dataType = "string")
    })
    @RequestMapping(value = "/push/template", method = RequestMethod.PUT)
    public ApiResponse updateAppPushTemplate(@ApiIgnore @Valid AppPushTemplateDto dto, String templateId) {
        return appPushTemplateInnerService.updateAppPushTemplate(dto, templateId);
    }

    @ApiOperation(value = "推送模版-删除", notes = "推送模板-删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "主键", paramType = "path", dataType = "string", required = true)
    })
    @RequestMapping(value = "/push/template/{templateId}", method = RequestMethod.DELETE)
    public ApiResponse deleteAppPushTemplate(@PathVariable("templateId") String templateId) {

        return appPushTemplateInnerService.deleteAppPushTemplate(templateId);
    }

    @ApiOperation(value = "推送模版-发布", notes = "推送模板-发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "主键", paramType = "form", dataType = "string", required = true)
    })
    @RequestMapping(value = "/push/send", method = RequestMethod.POST)
    public ApiResponse pushToJiGuangPaas(String templateId) {
        return appPushTemplateInnerService.pushToJiGuangPaas(templateId);
    }

    @ApiOperation(value = "推送模版-发布new", notes = "推送模板-发布new")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "主键", paramType = "form", dataType = "string", required = true)
    })
    @RequestMapping(value = "/push/sendnew", method = RequestMethod.POST)
    public ApiResponse pushToJiGuangPaasNew(String templateId) {
        return appPushTemplateInnerService.pushToJiGuangPaasNew(templateId);
    }

}